package viewer;

import graphics.Graphics3D;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.MouseInputListener;

import logger.Logger;
import panels.Canvas3D;
import classes.Matrix;

public class TestCamera3D extends JFrame implements MouseWheelListener, MouseMotionListener, MouseListener, MouseInputListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel 		contentPane;
	private	Canvas3D	canvas3D;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TestCamera3D frame = new TestCamera3D();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TestCamera3D() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		canvas3D = new Canvas3D();
		contentPane.add(canvas3D, BorderLayout.CENTER);
		
		canvas3D.addMouseWheelListener(this);
		canvas3D.addMouseMotionListener(this);
		canvas3D.addMouseListener(this);
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent e) {
		Logger.println("mouseWheelMoved");
		float zoom = 1f;
		if (e.getPreciseWheelRotation() < 0) {
			zoom = 1.2f;
		} else {
			zoom = 1f/1.2f;
		}
		Logger.println("zoom = " + zoom);
		getGraphics3D().push(Matrix.scaling(zoom, zoom, zoom));
		
		Logger.println(getGraphics3D().getCamera().niceString());
		refresh();
		
		
	}

	
	private	Point	mPos = new Point(0,0);
	
	@Override
	public void mouseDragged(MouseEvent e) {
		float	dx	= e.getPoint().x - mPos.x;
		float	dy	= e.getPoint().y - mPos.y;
		float	ry	= (float) -(dx * Math.PI / contentPane.getWidth());
		float	rx	= (float) -(dy * Math.PI / contentPane.getHeight());
		
		Logger.println(e.getButton());
		
		if (e.isControlDown()) {
			getGraphics3D().push(Matrix.rotationX(rx));
			getGraphics3D().push(Matrix.rotationY(ry));
		} else {
			getGraphics3D().push(Matrix.translation(dx, dy, 0f));	
		}
		
		mPos = e.getPoint();
		refresh();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		mPos = e.getPoint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {	}
	
	@Override
	public void mousePressed(MouseEvent e) {	}

	@Override
	public void mouseReleased(MouseEvent e) {	}

	@Override
	public void mouseEntered(MouseEvent e) {	}

	@Override
	public void mouseExited(MouseEvent e) {		}
	
	public void refresh() {
		this.contentPane.paintImmediately(0,0,getWidth(),getHeight());
	}
	
	public Graphics3D getGraphics3D() {
		return canvas3D.getGraphics3D();
	}

}
